package com.kedacom.freedomLabel.broadcastreceiver;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.kedacom.freedomLabel.Options;
import com.kedacom.freedomLabel.initdata.bean.EventBusMessage;
import com.kedacom.lego.message.LegoMessageManager;

public class NetBroadcastReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        // TODO: This method is called when the BroadcastReceiver is receiving
        // an Intent broadcast.
//        throw new UnsupportedOperationException("Not yet implemented");

        LegoMessageManager.getInstance().sendMessage(Options.NET_BROADCAST_RECEIVER,getNetworkConnectionType(context));

    }
//new EventBusMessage(9, "服务器接口测试成功", "测试", 1)
    public EventBusMessage getNetworkConnectionType(Context context) {
        EventBusMessage messageEvent =new EventBusMessage();
        //获取连接管理器
        final ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);

        if (connectivityManager == null){
            messageEvent.setDes("当前网络已断开");
            messageEvent.setName("断开");
            messageEvent.setSuccess(0);
            return messageEvent;
        }
        //获取网络连接信息
        NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
        if (networkInfo != null && networkInfo.isAvailable()) {
            if (networkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
                messageEvent.setDes("当前使用WIFI网络");
                messageEvent.setName("连接");
                messageEvent.setSuccess(1);
                return messageEvent;
//                return "当前使用WIFI网络";
            }
            if (networkInfo.getType() == ConnectivityManager.TYPE_MOBILE) {
                messageEvent.setDes("当前使用移动数据网络");
                messageEvent.setName("连接");
                messageEvent.setSuccess(1);
                return messageEvent;
//                return "当前使用移动数据网络";
            }
        }

        messageEvent.setDes("网络已断开");
        messageEvent.setName("断开");
        messageEvent.setSuccess(0);

        return messageEvent;
    }

}
